package kr.hkit.BusinessManagement.ViewUI;

import java.awt.BorderLayout;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

import kr.hkit.BusinessManagement.DBCon.ProductDBCon;
import kr.hkit.BusinessManagement.DTO.ProductDTO;

public class FrameProductLists extends JFrame {

	private JPanel contentPane;
	private ArrayList<ProductDTO> productLists = new ArrayList<>();
	private ProductDBCon DBCon = new ProductDBCon();

	/**
	 * Launch the application.
	 */
/*	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameBusinessEmpLists frame = new FrameBusinessEmpLists();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public FrameProductLists() {
		setTitle("제품현황");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		JLabel lblNewLabel = new JLabel("제품 리스트");
		lblNewLabel.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		contentPane.add(lblNewLabel, BorderLayout.NORTH);
		
		productLists= DBCon.selectAll();
		
		MyCustomModel CM = new MyCustomModel();
		JTable table = new JTable(CM);
		scrollPane.setViewportView(table);
		
		DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
		dtcr.setHorizontalAlignment(SwingConstants.CENTER);
		
		TableColumnModel alignModel = table.getColumnModel();
		alignModel.getColumn(0).setCellRenderer(dtcr);
		alignModel.getColumn(1).setCellRenderer(dtcr);
		alignModel.getColumn(2).setCellRenderer(dtcr);
		alignModel.getColumn(3).setCellRenderer(dtcr);
	}
	
	
class MyCustomModel extends AbstractTableModel{
		
		final String[] COLUMN_NAME = {"제품코드","제품이름","판매정가","판매원가"};
		final Class[] COLUMN_CLASS = {String.class, String.class, Integer.class, Integer.class};
		
		
		// 뿌려줄 자료형 
		@Override
		public Class<?> getColumnClass(int col) {
			return COLUMN_CLASS[col];
		}
		
		//컬럼 네임 (테이블 제목)
		@Override
		public String getColumnName(int col) {
			return COLUMN_NAME[col];
		}
		
		//칼럼 갯수
		@Override
		public int getColumnCount() {
			return COLUMN_NAME.length;
		}
		
		//자료(사원) 수 
		@Override 
		public int getRowCount() {
			return productLists.size();
		}

		@Override
		public Object getValueAt(int row, int col) {
			
			DecimalFormat df = new DecimalFormat("#,###");
			ProductDTO product = productLists.get(row);
			
			switch(col){
			case  0 : return product.getCode();
			case  1 : return product.getName();
			case  2 : return df.format(product.getSellPrice());
			case  3 : return df.format(product.getOfferPrice());
			 
			}
			return null;
		}
		
	}


}
